package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.CodeA0Dao;
import com.int_tech.MessageCodeAll.entity.CodeA0;
import com.int_tech.car.dao.CarDao;
import com.int_tech.car.entity.Car;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.utils.DateTool;
import com.int_tech.utils.TimePlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/6/23.
 */
@Component
public class A0ToNewMessionTimeTask {

    @Autowired
    private CodeA0Dao codeA0Dao;

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private CarDao carDao;


    //创建任务单
    @Scheduled(cron="0/10 * * * * ?")
    public void checkCreateNewMession() throws ParseException {

        List<CodeA0> codeA0List1 = codeA0Dao.findByStatusOrderByCreatedTimeAsc(1);
        List<CodeA0> codeA0List2 = codeA0Dao.findByStatusOrderByCreatedTimeAsc(-1);

        List<CodeA0> codeA0List = new ArrayList<>();
        codeA0List.addAll(codeA0List1);
        codeA0List.addAll(codeA0List2);


        for(CodeA0 codeA0 : codeA0List)
        {
            try {
                //首先去找相同车辆的上一个订单
                List<CodeA0> codeA0ListBefor = codeA0Dao.findByCarNumberAndCreatedTimeBefore(codeA0.getCarNumber(),codeA0.getCreatedTime());

                //如果存在之前的数据集合
                if(codeA0ListBefor == null || codeA0ListBefor.size() == 0)
                {
                    //上面没有订单，直接创建任务
                    codeA0.setStatus(3);

                    codeA0Dao.save(codeA0);

                    newAMesssion(codeA0);
                }
                else
                {
                    //上一个订单
                    int m = 0;
                    CodeA0 codeA0Before = codeA0ListBefor.get(m);

                    while (StringUtils.isEmpty(codeA0Before.getRate()))
                    {
                        m ++;
                        codeA0Before = codeA0ListBefor.get(m);
                    }

                    m = 0;

//                    System.out.print("1111111111111"+codeA0Before.getRate());
//                    System.out.print("2222222222"+codeA0.getRate());

                    //判定如果上一个订单与这个订单测量时间超过5分钟，或者转载率差距相差2以上，则创建任务，否则标记为-3任务
                    //时间差
                    Date date1 = codeA0.getCreatedTime();
                    Date date2 = codeA0Before.getCreatedTime();
                    Float shijiancha = Float.parseFloat(TimePlus.gongshiTimePlus(date1,date2,1));

                    //装载率差
                    String rate1 = codeA0.getRate();
                    String rate2 = codeA0Before.getRate();
                    Float ratePlus = Float.parseFloat(rate1) - Float.parseFloat(rate2);

                    //符合创建新任务条件，时间差大于5，装载率超过2%
                    if((shijiancha > 5) || ((ratePlus > 2 || ratePlus < -2)))
                    {
                        //直接创建任务
                        //标记为-3任务
                        codeA0.setStatus(3);

                        codeA0Dao.save(codeA0);

                        newAMesssion(codeA0);

                    }
                    else
                    {
                        //标记为-3任务
                        codeA0.setStatus(-3);

                        codeA0Dao.save(codeA0);
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                codeA0.setStatus(-33);
                codeA0Dao.save(codeA0);
            }
        }
    }


    public void newAMesssion(CodeA0 codeA0) {

        String messionNumber = "";

        String logData = codeA0.getDataString();

        String[] logs = logData.split(",");

        String carNumber = codeA0.getCarNumber();

        //任务状态0，单车同时只允许1个未完成任务
        int isOver = 0;
        Mession mession = null;

        //查询最大任务递增码
        //车牌区号
        String areaNumber = carNumber.substring(0,3);

        String time = DateTool.Date2String(codeA0.getCreatedTime(),"yyyyMMddHHmmss");
        time = time.substring(0, 8);

        int todayCount = messionDao.countByMessionNumberLike(time);

        int thisCount = todayCount + 1;

        String maxCode = String.valueOf(thisCount);


        if(thisCount < 10)
        {
            maxCode = "000"+String.valueOf(maxCode);
        }
        else if(thisCount < 100)
        {
            maxCode = "00"+String.valueOf(maxCode);
        }
        else if(thisCount < 1000)
        {
            maxCode = "0"+ String.valueOf(maxCode);
        }

        messionNumber = areaNumber+time+maxCode;
        mession = new Mession();
        mession.setMessionNumber(messionNumber);
        mession.setCreateTime(codeA0.getCreatedTime());
        mession.setUpdateTime(codeA0.getCreatedTime());
        mession.setCarNumber(carNumber);
        mession.setIsOver(0);
        mession.setMessionCount(thisCount);
        mession.setRate(codeA0.getRate());
        mession.setIsAnsyCarTime(-99);
        mession.setYiChang(0);
        mession.setCheckStatus(0);
        mession.setRateDataString(codeA0.getRateDateString());

        messionDao.save(mession);



        codeA0.setMessionNumber(messionNumber);

        codeA0Dao.save(codeA0);

//        mession.setIsInMession(socketLog1.getIsInMession());

        //长宽高
        float x = Float.parseFloat(logs[10]);
        float y = Float.parseFloat(logs[11]);
        float z = Float.parseFloat(logs[12]);

        //修改车厢长宽高
        float xx = x/100;
        float yy = y/100;
        float zz = z/100;

        Car car = carDao.findCarByCarNumberAndIsDel(carNumber,0);
        car.setLength(String.valueOf(xx));
        car.setWidth(String.valueOf(yy));
        car.setHeight(String.valueOf(zz));

        carDao.save(car);
    }
}
